<?php

namespace app\controller\common;
require_once __DIR__.'/../../common/AuthController.php';
use app\common\AuthController;
use think\Db;


/**
 * 公共API模块
 */
class Index extends AuthController
{   


    /**
     * 初始化操作
     */
    public function init()
    {

    }
	

    /**
     * 获取大区市列表
     */
	public function api_common_index_getCityList($msg)
    {   
        global $db;
        $info = [
            'page' =>  isset($msg['data']['page'])?$msg['data']['page']:1,
            'rows' => isset($msg['data']['rows'])?$msg['data']['rows']:15,
            'admin_region_id' => isset($msg['data']['admin_region_id'])?$msg['data']['admin_region_id']:'',
            'region_name' => isset($msg['data']['region_name'])?$msg['data']['region_name']:''
        ];
        $limit = ($info['page']-1)*$info['rows'].','.$info['rows'];
        if(empty($info['admin_region_id'])){
            return $this->outMessage(-1,'请选择大区');
        }
        $region_info = $this->table->find('admin_region',['id'=>$info['admin_region_id']]);
        $region_ids = $region_info['region_ids'];
        $where = '';
        if(!empty($info['region_name'])){
            $where = "AND region_name LIKE '%".$info['region_name']."%'";
        }
        $list = $db->query("SELECT id,region_name FROM region WHERE province_id in ($region_ids) ".$where." AND region_type = 2 limit ".$limit);
        $count =  $db->query("SELECT count(*) as count from region WHERE province_id in ($region_ids) ".$where." AND region_type = 2");
        return $this->outMessage(10000,'获取成功',$list,'success',$count[0]['count']);
	}


    
    /**
     * 创建进出明细
     */
    public function api_common_index_createFlow($data)
    {
        if(!is_array($data) || empty($data)){
            return false;
        }
        if(!is_array($data['goods_list']) || empty($data['goods_list'])){
            return false;
        }
        foreach ($data['goods_list'] as $k => $v) {
            $res = $this->table->insert('depot_goods',$v);
            if(!$res){
                return false;
            }
            $info = $this->table->find('depot_stock',['depot_id'=> $data['depot_id'],'goods_id'=>$v['goods_id']]);
            //如果库存里面不存在该商品就去创建一个
            if(empty($info)){
                $goods = $this->table->find('goods',['id'=>$v['goods_id']]);
                $stock_data = [
                    'depot_id' => $data['depot_id'],
                    'depot_name' => $data['depot_name'],
                    'goods_id' => $v['goods_id'],
                    'goods_code' => $goods['code'],
                    'goods_name' => $goods['name'],
                    'goods_pic' => $goods['main_pic'],
                    'brand_id' => $goods['brand_id'],
                    'brand_name' => $goods['brand_name'],
                    'category_id' => $goods['category_id_2'],
                    'category_name' => $goods['category_name_2'],
                    'supplier_id' => $goods['supplier_id'],
                    'supplier_name' => $goods['supplier_name'],
                    'attribute' => $goods['attribute'],
                    'units_id' => $goods['units_id'],
                    'units_name' => $goods['units_name'],
                    'price' => $goods['buy_price'],
                    'create_time' => date('Y-m-d H:i:s',time())
                ];
                $this->table->insert('depot_stock',$stock_data);
            }
            $stock_list[$k]['depot_id'] = $data['depot_id'];
            $stock_list[$k]['goods_id'] = $v['goods_id'];
            $stock_list[$k]['number'] = $v['number'];
        }
        unset($data['goods_list']);
        $res = $this->table->insert('depot_flow_log',$data);
        if(!$res){
            return false;
        }
        //对库存进行修改
        $this->stockEdit($stock_list);
	    return true;
    }




    /**
     * 对库存进行修改
     */
    protected function stockEdit($data,$type = 1){
        global $db;
        if(!is_array($data) || empty($data)){
            return false;
        }
        foreach ($data as $v) {
            $info = $this->table->find('depot_stock',['depot_id'=>$v['depot_id'],'goods_id'=>$v['goods_id']]);
            $stock = $type == 1?$info['stock'] + $v['number']:$info['stock'] - $v['number'];
            $res = $this->table->update('depot_stock',['depot_id'=>$v['depot_id'],'goods_id'=>$v['goods_id']],['stock' =>$stock]);
            if(!$res){
                return false;
            }
        }
        return true;
    }



    /**
     * 测试一下
     */
    public function api_common_index_demo($msg)
    {
        $list = Db::name('goods_adjust_price_list')->field('order_no,id,goods_id,goods_name')->limit(0,2)->select();
        $count = Db::name('goods_adjust_price_list')->count();
        return $this->outMessage(10000,'获取成功',$list,'success',$count);
    }

}


